<?php

class LugaralmcController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
     	# $this->_helper->layout->disableLayout ();
		//$this->_helper->viewRenderer->setNoRender ();
		 echo "indexActionlugaralmc";
	  
    }
	
	public function lugaralmcAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());
	}
	public function buscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			$chk = $this->_request->getPost('chk');
			$cn = new Model_Data();
			$parameter[]=array("@pcod_lugalm",($tip=='0' ? $val:''));
			$parameter[]=array("@pdesc_lugalm",($tip=='1' ? $val:''));
			$parameter[]=array("@pdesc_tiplugar",($tip=='2' ? $val:''));
			$parameter[]=array("@pest_lugalm",$chk);
			$result_bus = $cn ->ejec_store_procedura_sql("MLugarAlmacenamiento_list", $parameter);

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'">';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[2].'</td>';
					$tb .='<td>'.$value[4].'</td>';
					$tb .='<td>'.$value[5].'</td>';
					$tb .='<td>'.$value[7].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<tr class="ui-widget-header">'.
									'<th width="100px">Codido</th>'.
									'<th width="150px">Almacen</th>'.
									'<th width="100px">Sector</th>'.
									'<th width="130px">Lugar de Almacenamiento</th>'.
									'<th width="100px">Tipo</th>'.
								'</tr>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenu();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	
	public function mantenimientoAction(){
		
     	echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$cod = $this->_request->getParam('codItem', '');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		$parameter[]=array("@pcod_lugalm",$cod);
		$parameter[]=array("@pdesc_lugalm",'');
		$parameter[]=array("@pdesc_tiplugar",'');
		$parameter[]=array("@pest_lugalm",'');

		$CBparameter[]=array("@pcod_almacen","");
		$CBparameter[]=array("@pdesc_almacen",'');
		$CBparameter[]=array("@pest_almacen",'1');
		
		$CBTipoparameter[]=array("@pcod_tiplugar",'');
		$CBTipoparameter[]=array("@pdesc_tipligar",'');
		$CBTipoparameter[]=array("@pest_tiplugar",'');
		
		$CBresult_bus = $cn ->ejec_store_procedura_sql("MAlmacen_list", $CBparameter);
		for ($x=0;$x<count($CBresult_bus);$x++)
    		$cboCB[] = array($CBresult_bus[$x][0],$CBresult_bus[$x][1]);
		
		$CBTiporesult_bus = $cn ->ejec_store_procedura_sql("MTipoLugar_list", $CBTipoparameter);
		for ($y=0;$y<count($CBTiporesult_bus);$y++)
    		$cboCBTipo[] = array($CBTiporesult_bus[$y][0],$CBTiporesult_bus[$y][1]);
		
		if ($type=='N'){
			$val[] = array('#cbocod_almacen',$pintar->ContenidoCombo($cboCB,'null'),'html');
			$val[] = array('#cbocod_tiplugar',$pintar->ContenidoCombo($cboCBTipo,'null'),'html');
		}elseif($type=='M' || $type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("MLugarAlmacenamiento_list", $parameter);
			$val[] = array('#hdcodigo',$cod,'val');
			$val[] = array('#cbocod_almacen',$pintar->ContenidoCombo($cboCB,$result_bus[0][1]),'html');
			$hab[] = array('#cbocod_almacen',true);
			$pintar->HabilitarComponente($hab);
			$Secparameter[]=array("@pcod_sector",'');
			$Secparameter[]=array("@pdesc_sector",'');
			$Secparameter[]=array("@pest_sector",'1');
			$Secparameter[]=array("@pcod_almacen",$result_bus[0][1]);
			$Secresult_bus = $cn ->ejec_store_procedura_sql("MSector_list", $Secparameter);
			$pintar = New Libreria_Pintar();
			for ($x=0;$x<count($Secresult_bus);$x++)
    			$cboSec[] = array($Secresult_bus[$x][0],$Secresult_bus[$x][3]);
			
			$val[] = array('#cbocod_sector',$pintar->ContenidoCombo($cboSec,$result_bus[0][3]),'html');
			$val[] = array('#cbocod_tiplugar',$pintar->ContenidoCombo($cboCBTipo,$result_bus[0][6]),'html');
			$val[] = array('#txtds_lugalm',$result_bus[0][5],'val');
			$val[] = array('#txtds_observ',$result_bus[0][8],'val');
			

			$parameterdt[]=array("@pcod_divi",'');
			$parameterdt[]=array("@pdesc_divi",'');
			$parameterdt[]=array("@pest_divi",'1');
			$parameterdt[]=array("@pcod_lugalm",$cod);
			
			$result_det = $cn ->ejec_store_procedura_sql("MDivision_list", $parameterdt);
			$detallemp='';
			if (count($result_det)==0){
				
			}else{
			for ($i=0;$i<count($result_det);$i++){
				$detallemp .=
				'<tr >'.
					'<td>'.
						'<input type="text"  name="det['.$i.'][txt_descrip]" value="'.$result_det[$i][2].'"  class="ui-widget-content ui-corner-all">'.
					'</td>'.
					'<td>'.
						'<input type="text"  name="det['.$i.'][txt_obs]"  value="'.$result_det[$i][3].'" class="ui-widget-content ui-corner-all">'.							
					'</td> '.
					'<td style="display:none" ><input  name="det['.$i.'][tblcodigo]" id="tblcodigo" type="text" value="'.$result_det[$i][0].'"></td>'.
				'</tr>';
			}
				$val[] = array('#tbl_detalleLG tbody',$detallemp,'html');
				
			}
		
		}
		
		$pintar->PintarValor($val);
		
		echo '<script type="text/javascript">'.
						'$("#tbl_detalleLG").grider({'.
						'countRow : true,'.
						'countRowAdd : true'.
						"});</script>";
	}

	public function executemanteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$hdcodigo = $upper->filter($this->_request->getPost('hdcodigo'));
			$cbocod_almacen = $upper->filter($this->_request->getPost('cbocod_almacen'));
			$cbocod_sector = $upper->filter($this->_request->getPost('cbocod_sector'));
			
			$cbocod_tiplugar = $upper->filter($this->_request->getPost('cbocod_tiplugar'));
			$txtds_lugalm = $upper->filter($this->_request->getPost('txtds_lugalm'));
			$txtds_observ = $upper->filter($this->_request->getPost('txtds_observ'));
			$st_estado = $upper->filter($this->_request->getPost('st_stado'));
			$arrtable = $this->_request->getPost('det');
			
			$cn = new Model_Data();
			
			if($tip=='M' ||$tip=='N' ){
				$parameter[]=array("@ptype",$tip);
				/*@pcod_lugalm  char(5),      
					@pcod_sector  varchar(150),      
					@pcod_tiplugar varchar(250),      
					@pdesc_lugalm varchar(250),      
					@pobs_lugalm varchar(250),
					@pest_lugalm  char(1),     
					@pdetalle varchar(max), */
				$parameter[]=array("@pcod_lugalm",$hdcodigo);
				$parameter[]=array("@pcod_sector",$cbocod_sector);
				$parameter[]=array("@pcod_tiplugar",$cbocod_tiplugar);
				$parameter[]=array("@pdesc_lugalm" ,$txtds_lugalm);
				$parameter[]=array("@pobs_lugalm" ,$txtds_observ);
				$parameter[]=array("@pest_lugalm" ,$st_estado);
				
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$xml='';
				$i=1;
				foreach ($arrtable as $value) {
					$xml.='';
					$xml .='<r ';
					$xml .='xcodigo="'.$value["tblcodigo"].'" ';
					$xml .='xcorrela="'.($i).'" ';
					$xml .='xcod_lugalm="'.$hdcodigo.'" ';
					$xml .='xdesc_divi="'.$value["txt_descrip"].'" ';
					$xml .='xobs_divi="'.$value["txt_obs"].'" ';
					$xml .='>';
					$xml .='</r>';
					$i++;
				}
				
				$xml="<d>".$xml."</d>";
				//echo "<textarea>".$xml."</textarea>";
				$parameter[]=array("@pdetalle",$xml);
				$result = $cn ->ejec_store_procedura_sql("MLugarAlmacenamiento_InsertUpdate", $parameter);
				 	
			}elseif($tip=='D'){
	
				$parameter[]=array("@pcod_lugalm",$hdcodigo);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("MLugarAlmacenamiento_Delete", $parameter);
			}
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}

}

